<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <?= $show_code; ?>
        <title><?php echo $this->config->item('title_page'); ?></title>
    </head>
    <script >
        $(document).ready(function() {

            $('#btnBackPage').click(function() {
                parent.history.back();
                return false;
            });

            var lastIndex = 0;
            var arrayConfirm = [];

            $("#btn_add").click(function() {
                set_grid_form();
            });

            function set_grid_form() {

                var listMainType = <?php echo json_encode($list_main_type); ?>;
                var strCmbMainType = '<select id="cmbMainType' + lastIndex + '" name="cmbMainType' + lastIndex + '" style="height:27px;width:200px">';
                $.each(listMainType, function(key, value) {
                    if (key === "") {
                        strCmbMainType += '<option value="' + key + '" selected>' + value + '</option>';
                    } else {
                        strCmbMainType += '<option value="' + key + '">' + value + '</option>';
                    }
                });
                strCmbMainType += '</select>';

                var listSubType = <?php echo json_encode($list_sub_type); ?>;
                var strCmbSubType = '<select  id="cmbSubType' + lastIndex + '" name="cmbSubType' + lastIndex + '" style="height:27px;width:200px">';
                $.each(listSubType, function(key, value) {
                    if (key === "") {
                        strCmbSubType += '<option value="' + key + '" selected>' + value + '</option>';
                    } else {
                        strCmbSubType += '<option value="' + key + '">' + value + '</option>';
                    }
                });
                strCmbSubType += '</select>';

                var listBrand = <?php echo json_encode($list_brand); ?>;
                var strCmbBrand = '<select id="cmbBrand' + lastIndex + '" name="cmbBrand' + lastIndex + '" style="height:27px;width:100px">';
                $.each(listBrand, function(key, value) {
                    if (key === "") {
                        strCmbBrand += '<option value="' + key + '" selected>' + value + '</option>';
                    } else {
                        strCmbBrand += '<option value="' + key + '">' + value + '</option>';
                    }
                });
                strCmbBrand += '</select>';


                $("#table_autoparts").append('<tr id="tr_row' + lastIndex + '">\n\
                                        <td>\n\
                                            ' + (lastIndex + 1) + '\n\
                                        </td>\n\
                                        <td>\n\
                                            <input type="text" id="txtLocateProcure' + lastIndex + '" name="txtLocateProcure' + lastIndex + '" />\n\
                                             <div id="lblLocateProcure' + lastIndex + '" style="display: none;height:22px;"></div>\n\
                                        </td>\n\
                                        <td>\n\
                                            ' + strCmbMainType + '\n\
                                             <div id="lblMainType' + lastIndex + '" style="display: none;height:22px;"></div>\n\
                                        </td>\n\
                                        <td>\n\
                                            ' + strCmbSubType + '\n\
                                             <div id="lblSubType' + lastIndex + '" style="display: none;height:22px;"></div>\n\
                                        </td>\n\
                                        <td>\n\
                                            ' + strCmbBrand + '\n\
                                             <div id="lblBrand' + lastIndex + '" style="display: none;;height:22px;"></div>\n\
                                        </td>\n\
                                        <td>\n\
                                            <input list="partsName' + lastIndex + '" id="txtAutoPart' + lastIndex + '" name="txtAutoPart' + lastIndex + '" disabled>\n\
                                            <div id="lblAutoPart' + lastIndex + '" style="display: none;height:22px;"></div>\n\
                                            <datalist id="partsName' + lastIndex + '">\n\
                                            </datalist>\n\
                                        </td>\n\
                                        <td>\n\
                                            <input type="number" id="txtQty' + lastIndex + '" name="txtQty' + lastIndex + '" style="width:50px">\n\
                                            <div id="lblQty' + lastIndex + '" style=""display: none;height:22px;width:50px" ></div>\n\
                                        </td>\n\
                                        <td>\n\
                                            <input id="txtPrice' + lastIndex + '" name="txtPrice' + lastIndex + '" style="width:80px">\n\
                                            <div id="lblPrice' + lastIndex + '" style=""display: none;height:22px" ></div>\n\
                                        </td>\n\
                                        <td>\n\
                                            <button type="button" id="btnConfirm' + lastIndex + '" name="btnConfirm' + lastIndex + '" class="btn btn-success btn-sm">\n\
                                                <span class="glyphicon glyphicon-ok"></span>\n\
                                            </button>\n\
                                            <button type="button" id="btnEdit' + lastIndex + '" name="btnEdit' + lastIndex + '" class="btn btn-success btn-sm" style="display:none">\n\
                                                <span class="glyphicon glyphicon-edit"></span>\n\
                                            </button>\n\
                                            <button type="button" id="btnDelete' + lastIndex + '" name="btnDelete' + lastIndex + '" class="btn btn-danger btn-sm" >\n\
                                                <span class="glyphicon glyphicon-trash"></span>\n\
                                            </button>\n\
                                        </td>\n\
                                    </tr>');

                $('#cmbMainType' + lastIndex).on('change', {value: lastIndex}, function(event) {
                    set_list('main', event.data.value);
                });

                $('#cmbSubType' + lastIndex).on('change', {value: lastIndex}, function(event) {
                    set_list('sub', event.data.value);
                });

                $('#cmbBrand' + lastIndex).on('change', {value: lastIndex}, function(event) {
                    set_list('brand', event.data.value);
                });

                $('#btnConfirm' + lastIndex).on('click', {value: lastIndex}, function(event) {
                    btnConfirm(event.data.value);
                });

                $('#btnEdit' + lastIndex).on('click', {value: lastIndex}, function(event) {
                    btnEdit(event.data.value);
                });

                $('#btnDelete' + lastIndex).on('click', {value: lastIndex}, function(event) {
                    btnDelete(event.data.value);
                });

                lastIndex = lastIndex + 1;
            }

            function btnConfirm(index) {
                var valueLocateProcure = $('#txtLocateProcure' + index).val();
                var valueMainType = $('select[name=cmbMainType' + index + '] option:selected').text();
                var valueSubType = $('select[name=cmbSubType' + index + '] option:selected').text();
                var valueBrand = $('select[name=cmbBrand' + index + '] option:selected').text();
                var valueMainTypeId = $('select[name=cmbMainType' + index + '] option:selected').val();
                var valueSubTypeId = $('select[name=cmbSubType' + index + '] option:selected').val();
                var valueBrandId = $('select[name=cmbBrand' + index + '] option:selected').val();
                var valueAutoPart = $('#txtAutoPart' + index).val();
                var valueQty = $('#txtQty' + index).val();
                var valuePrice = $('#txtPrice' + index).val();
                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>repair/add_repair/chk_autopart",
                    data: {name: valueAutoPart, qty: valueQty, main_type_id: valueMainTypeId, sub_type_id: valueSubTypeId, brand_id: valueBrandId},
                    dataType: "json",
                    success: function(data) {
                        if (valueLocateProcure !== "" && valueMainType !== "เลือก" && valueSubType !== "เลือก"
                                && valueBrand !== "เลือก" && valueAutoPart !== "" && valueQty !== 0 && valueQty !== "" && valuePrice !== "" && valuePrice !== 0) {
                            if (data.result_name === "TRUE") {
                                $('#lblLocateProcure' + index).text(valueLocateProcure);
                                $('#lblMainType' + index).text(valueMainType);
                                $('#lblSubType' + index).text(valueSubType);
                                $('#lblBrand' + index).text(valueBrand);
                                $('#lblAutoPart' + index).text(valueAutoPart);
                                $('#lblQty' + index).text(valueQty);
                                $('#lblPrice' + index).text(valuePrice);

                                $('#lblLocateProcure' + index).css('display', 'block');
                                $('#txtLocateProcure' + index).css('display', 'none');
                                $('#lblMainType' + index).css('display', 'block');
                                $('#cmbMainType' + index).css('display', 'none');
                                $('#lblSubType' + index).css('display', 'block');
                                $('#cmbSubType' + index).css('display', 'none');
                                $('#lblBrand' + index).css('display', 'block');
                                $('#cmbBrand' + index).css('display', 'none');
                                $('#lblAutoPart' + index).css('display', 'block');
                                $('#txtAutoPart' + index).css('display', 'none');
                                $('#lblQty' + index).css('display', 'block');
                                $('#txtPrice' + index).css('display', 'none');
                                $('#lblPrice' + index).css('display', 'block');
                                $('#txtQty' + index).css('display', 'none');
                                $('#btnConfirm' + index).css('display', 'none');
                                $('#btnEdit' + index).css('display', 'inline-block');
                                arrayConfirm.push(index);
                                $('#hidArrayConfirm').val(arrayConfirm);
                            } else {
                                alert('ชื่ออะไหล่ไม่มีในระบบ กรุณาตรวจสอบอีกครั้ง');
                            }
                        } else {
                            alert('โปรดตรวจสอบข้อมูล');
                        }
                    }
                });
            }
            function btnEdit(index) {

                $('#lblLocateProcure' + index).css('display', 'none');
                $('#txtLocateProcure' + index).css('display', 'block');
                $('#lblMainType' + index).css('display', 'none');
                $('#cmbMainType' + index).css('display', 'block');
                $('#lblSubType' + index).css('display', 'none');
                $('#cmbSubType' + index).css('display', 'block');
                $('#lblBrand' + index).css('display', 'none');
                $('#cmbBrand' + index).css('display', 'block');
                $('#lblAutoPart' + index).css('display', 'none');
                $('#txtAutoPart' + index).css('display', 'block');
                $('#lblQty' + index).css('display', 'none');
                $('#txtQty' + index).css('display', 'block');
                $('#lblPrice' + index).css('display', 'none');
                $('#txtPrice' + index).css('display', 'block');
                $('#btnConfirm' + index).css('display', 'inline-block');
                $('#btnEdit' + index).css('display', 'none');
                arrayConfirm.splice($.inArray(index, arrayConfirm), 1);
                $('#hidArrayConfirm').val(arrayConfirm);
            }

            function btnDelete(index) {
                if ((lastIndex - 1) === index) {
                    if (confirm('ยืนยันการลบข้อมูล?') === true) {
                        $("#tr_row" + index).remove();
                        arrayConfirm.splice($.inArray(index, arrayConfirm), 1);
                        $('#hidArrayConfirm').val(arrayConfirm);
                        lastIndex = lastIndex - 1;
                    } else {
                        return false;
                    }
                } else {
                    alert('โปรดลบรายการสุดท้าย');
                }
            }

            function set_list(type, index) {
                if (type === "main") {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>stock/add_stock/get_sub_type",
                        data: {id: $("#cmbMainType" + index).val()},
                        dataType: "json",
                        success: function(data) {
                            $("#cmbSubType" + index).find("option").remove();
                            $("#cmbBrand" + index).find("option").remove();
                            $('#txtAutoPart' + index).attr('disabled', true);
                            if (data.length > 0) {
                                var opt = $('<option />');
                                opt.val("");
                                opt.text(data.list_model[""]);
                                $('#cmbSubType' + index).append(opt);
                                var opt = $('<option />');
                                opt.val("");
                                opt.text(data.list_model[""]);
                                $('#cmbBrand' + index).append(opt);
                                $.each(data.list_model, function(key, val) {
                                    if (key !== "") {
                                        var opt = $('<option />');
                                        opt.val(key);
                                        opt.text(val);
                                        $('#cmbSubType' + index).append(opt);
                                    }
                                });
                            }
                        }
                    });
                } else if (type === "sub") {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>stock/add_stock/get_brand",
                        data: {id: $('#cmbSubType' + index).val()},
                        dataType: "json",
                        success: function(data) {
                            $("#cmbBrand" + index).find("option").remove();
                            $('#txtAutoPart' + index).attr('disabled', true);
                            if (data.length > 0) {
                                var opt = $('<option />');
                                opt.val("");
                                opt.text(data.list_model[""]);
                                $('#cmbBrand' + index).append(opt);
                                $.each(data.list_model, function(key, val) {
                                    if (key !== "") {
                                        var opt = $('<option />');
                                        opt.val(key);
                                        opt.text(val);
                                        $('#cmbBrand' + index).append(opt);
                                    }
                                });
                            }
                        }
                    });
                } else if (type === "brand") {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>stock/add_stock/get_list_stock",
                        data: {brand_id: $('#cmbBrand' + index).val(), sub_id: $('#cmbSubType' + index).val(), main_id: $('#cmbMainType' + index).val()},
                        dataType: "json",
                        success: function(data) {
                            $('#partsName' + index).find("option").remove();
                            $('#txtAutoPart' + index).attr('disabled', true);
                            $('#txtAutoPart' + index).val('');
                            if (data.length > 0) {
                                $('#txtAutoPart' + index).attr('disabled', false);
                                var opt = $('<option />');
                                opt.val("");
                                opt.text(data.list_model[""]);
                                $('#partsName' + index).append(opt);
                                $.each(data.list_model, function(key, val) {
                                    var opt = $('<option />');
                                    opt.val(val);
                                    $('#partsName' + index).append(opt);
                                });
                            }
                        }
                    });
                }
            }
        });
    </script>
    <body>
        <div class="panel panel-default body-contain">
            <div class="panel-heading"><span class="icon-g-barcode"></span>
                <strong> การจัดการรายการซื้อ (Purchase Management)</strong>
            </div>
            <div class="panel-body">
                <?php $this->load->view('contains/menu_module_bar'); ?>
                <?php echo form_open('purchase/add_purchase/validate', 'class="form-horizontal"  method="post"') ?>
                <input type="hidden" id="hidArrayConfirm" name="hidArrayConfirm" value=""/>
                <input type="hidden" id="hidResult"  value=""/>
                <div class="panel panel-default">
                    <div class="panel-heading"><span class="glyphicon glyphicon-file"></span>
                        <strong> เพิ่มข้อมูลรายการซื้อ</strong>
                    </div>
                    <div class="panel-body">
                        <div class="col-xs-12 col-md-8">
                            <div class="form-group">
                                <div>
                                    <label class="col-sm-3 control-label">วันที่ทำรายการ</label>
                                    <div class="col-sm-2">
                                        <?php echo $cmb_day; ?>
                                    </div>
                                    <div class="col-sm-3">
                                        <?php echo $cmb_month; ?>
                                    </div>
                                    <div class="col-sm-2">
                                        <?php echo $cmb_year; ?>
                                    </div>
                                </div>
                                <div>
                                    <label class="col-sm-3 control-label"></label>
                                    <div class="col-sm-5">
                                        <?php echo $err_day; ?>
                                        <?php echo $err_month; ?>
                                        <?php echo $err_year; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="panel-body" style="margin-top: -20px">
                        <div class="panel panel-default">
                            <!-- Default panel contents -->
                            <div class="panel-heading"><strong>รายการอะไหล่</strong>
                                <div class="pull-right">
                                    <button id="btn_add" type="button" class="btn btn-primary position-search-btn btn-sm" style="margin-top: -5px;margin-right: -10px">
                                        <span class="glyphicon glyphicon-plus"></span> 
                                        <strong>เพิ่มรายการ</strong>
                                    </button>
                                </div>
                            </div>

                            <!-- Table -->
                            <table class="table table-condensed table-hover table-bordered imagetable">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>สถานที่จัดซื้อ</th>
                                        <th>ประเภทหลัก</th>
                                        <th>ประเภทย่อย</th>
                                        <th>ยี่ห้อ</th>
                                        <th>ชื่ออะไหล่</th>
                                        <th>จำนวน</th>
                                        <th>ราคาต่อหน่วย</th>
                                        <th>การจัดการ</th>
                                    </tr>
                                </thead>
                                <tbody id="table_autoparts">

                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="form-group"  style="margin-top: -20px">
                        <div class="text-center">
                            <button type="submit" class="btn btn-primary">บันทึกข้อมูล</button>
                            <button id="btnBackPage" type="button" class="btn btn-default">ย้อนกลับ</button>
                        </div>
                    </div>
                </div>
                <?php echo form_close(); ?>
            </div>
        </div>
    </body>
</html>
